﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.BLL;
using Yipusoft.BaseDoc.Model;
using System.IO;
using Yipusoft.BaseDoc.BLL;

public partial class Task_BaseDoc_DocDepartmentList : System.Web.UI.Page
{
    List<MenuItem> menulist = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        Glist = new List<DocumentFileInfo>();
        menulist = new List<MenuItem>();
        if (!IsPostBack)
        {
            BindMonthList();
            BindYearList();
            bindDepartment();
            if (Request["DepartmentID"] != null)
            {
                string depart = Request["DepartmentID"];
                ddlDepartment.SelectedValue = Request["DepartmentID"].Substring(0, 4) + "000";
                bindSelection();
                ddlSection.Enabled = true;
                ddlSection.SelectedValue = depart;
                ddlSection.Enabled = false;
            }

            ListPageContral1.SetFirstPage();
            Bind();
            bindSubject();
        }
    }
    List<DocumentFileInfo> Glist = null;
    #region
    private void BindYearList()
    {
        for (int i = 2009; i <= 2050; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "年";
            li.Value = i.ToString();
            YearList.Items.Add(li);
        }
        YearList.SelectedValue = DateTime.Now.Year.ToString();

        if (Request["Year"] != null)
        {
            YearList.SelectedValue = Request["Year"];
        }
        if (Request["SM"] != null)
        {
            StartMonth.SelectedValue = Request["SM"];

        }
        if (Request["EM"] != null)
        {
            EndMonth.SelectedValue = Request["EM"];
            YearList.Enabled = false;
            StartMonth.Enabled = false;
            EndMonth.Enabled = false;
        }
        else
        {
            YearList.Enabled = true;
            StartMonth.Enabled = true;
            EndMonth.Enabled = true;
        }
    }

    private void BindMonthList()
    {
        for (int i = 1; i <= 12; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "月";
            li.Value = i.ToString();
            EndMonth.Items.Add(li);
        }
        EndMonth.SelectedValue = DateTime.Now.Month.ToString();
        for (int i = 1; i <= 12; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "月";
            li.Value = i.ToString();
            StartMonth.Items.Add(li);
        }
        StartMonth.SelectedValue = DateTime.Now.Month.ToString();
    }
    #endregion
    string DepartmentID = "";
    private void Bind()
    {
        Glist.Clear();
        menulist.Clear();
        MenuItem item = new MenuItem();
        item.Text = "根目录";
        item.Value = Guid.Empty.ToString();
        item.NavigateUrl = "DocDepartmentList1.aspx";
        //menupath.Items.Add(item);
        lblDocSum.Text = "0";

        if (Request["ID"] == null && Request["ID"] != "00000000-0000-0000-0000-000000000000")
        {

            string StartDate = YearList.SelectedValue + "-" + StartMonth.SelectedValue + "-1";
            string EndDate = YearList.SelectedValue + "-" + EndMonth.SelectedValue + "-1";
            EndDate = Convert.ToDateTime(EndDate).AddMonths(1).ToString();

            string DepartmentID = ddlSection.SelectedValue;
            string where = " DepartmentID='" + DepartmentID + "' And ParentID='" + Guid.Empty + "' and ((StartDate>='" + StartDate + "' and StartDate<='" + EndDate + "') or (StartDate<'" + EndDate + "' and EndDate>='" + EndDate + "')) Order By OldFileName";
            List<DocumentFileInfo> list = Yipusoft.BaseDoc.BLL.FileManager.GetList(where);
            int mun = 1;
            foreach (DocumentFileInfo info in list)
            {
                info.OldFileName = " <img src= \"../../Images/ico/" + info.Format + ".gif\" />" + info.OldFileName;
                info.Num = mun;
                mun++;
                Glist.Add(info);
                OrderItem(info.ID);
            }
            filepath.InnerHtml = "<strong>路径: </strong>";
        }
        else if (Request["ID"] == "00000000-0000-0000-0000-000000000000")
        {
            tempOrderItem(Request["year"], Request["SM"]);

            string temp = string.Empty;
            temp = "<strong>路径: </strong><a href='DocDepartmentList.aspx'>根目录</a>\\";

            temp += "<a href='DocDepartmentList1.aspx?Year=" + YearList.SelectedValue + "&SM=" + StartMonth.SelectedValue + "&EM=" + EndMonth.SelectedValue + "&ID=00000000-0000-0000-0000-000000000000&DepartmentID=" + ddlSection.SelectedValue + "'>临时文件夹</a>\\";

            filepath.InnerHtml = temp;
            gvwFileList.Caption = "<span>档案列表</span>";
        }
        else
        {
            OrderItem(new Guid(Request["ID"]));
            SetMenu(new Guid(Request["ID"]));
            string temp = string.Empty;
            temp = "<strong>路径: </strong><a href='DocDepartmentList.aspx'>根目录</a>\\";
            int mun = 1;
            if (menulist.Count > 0)
            {
                int count = menulist.Count;

                for (int i = count - 1; i >= 0; i--)
                {

                    temp += "<a href='DocDepartmentList1.aspx?Year=" + YearList.SelectedValue + "&SM=" + StartMonth.SelectedValue + "&EM=" + EndMonth.SelectedValue + "&ID=" + menulist[i].Value + "&DepartmentID=" + ddlSection.SelectedValue + "'>" + menulist[i].Text + "</a>\\";
                }
            }
            filepath.InnerHtml = temp;
        }
        gvwFileList.DataSource = Glist;
        ListPageContral1.ItemCount = Glist.Count();
        gvwFileList.DataBind();
    }

    private void tempOrderItem(string Year, string Month)
    {
        string DepartmentID = ddlSection.SelectedValue;
        string where = " Departmentid='" + DepartmentID + "' and ParentID='00000000-0000-0000-0000-000000000000' and year(StartDate)=" + Year + " and Month(StartDate)=" + Month + " and isFolder=0  Order by OldFileName ";


        List<Yipusoft.BaseDoc.Model.DocumentFileInfo> list = Yipusoft.BaseDoc.BLL.FileManager.GetList(where);
        int mun = 1;
        if (list != null)
        {
            foreach (DocumentFileInfo info in list)
            {
                info.OldFileName = " <img src= \"../../Images/ico/" + info.Format + ".gif\" />" + info.OldFileName;
                info.Num = mun;
                mun++;
                var pp = from o in Glist where info.ID == o.ID select o;
                if (pp.Count() == 0)
                {
                    Glist.Add(info);
                }
                else
                {

                }
                OrderItem(info.ID);
            }
        }


        list = Yipusoft.BaseDoc.BLL.FileManager.GetList(where);
        if (list != null)
        {
            foreach (DocumentFileInfo info in list)
            {
                if (!info.IsFolder)
                {
                    lblDocSum.Text = Convert.ToString(Convert.ToInt32(lblDocSum.Text) + 1);
                }
            }
        }
    }

    private void SetMenu(Guid ID)
    {
        DocumentFileInfo dinfo = Yipusoft.BaseDoc.BLL.FileManager.GetList("ID='" + ID + "'").FirstOrDefault();
        if (dinfo != null)
        {
            MenuItem item = new MenuItem();
            item.Text = dinfo.OldFileName;
            item.Value = dinfo.ID.ToString();
            item.NavigateUrl = "DocDepartmentList1.aspx?Year=" + YearList.SelectedValue + "&SM=" + StartMonth.SelectedValue + "&EM=" + EndMonth.SelectedValue + "&ID=" + dinfo.ID.ToString() + "&DepartmentID=" + ddlSection.SelectedValue;
            menulist.Add(item);
            SetMenu(dinfo.ParentID);
        }

    }
    public string GetChildCount(object objID)
    {
        if (objID == null)
            return "0";
        int temp = Yipusoft.BaseDoc.BLL.FileManager.GetChildFileCount((Guid)objID, true);
        if (Yipusoft.BaseDoc.BLL.FileManager.GetFile((Guid)objID).ParentID.Equals(Guid.Empty))
            return "<font color=red>" + temp + "</font>";
        else
            return temp.ToString();

    }
    public void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        gvwFileList.PageIndex = PageIndex;
        Bind();
    }
    private void OrderItem(Guid ID)
    {
        string FileCountwhere = "  ParentID='" + ID + "' Order by OldFileName ";
        string where = string.Empty;
        if (Request["ID"] == null)
            where = " IsFolder='True'  And ParentID='" + ID + "' Order by OldFileName ";
        else
        {
            where = "  ParentID='" + ID + "' Order by OldFileName ";

        }
        List<Yipusoft.BaseDoc.Model.DocumentFileInfo> list = Yipusoft.BaseDoc.BLL.FileManager.GetList(where);
        if (list != null)
        {
            int mun = 1;
            foreach (DocumentFileInfo info in list)
            {
                info.OldFileName = " <img src= \"../../Images/ico/" + info.Format + ".gif\" />" + info.OldFileName;
                info.Num = mun;
                mun++;
                Glist.Add(info);
                OrderItem(info.ID);
            }
        }
        list = Yipusoft.BaseDoc.BLL.FileManager.GetList(FileCountwhere);
        if (list != null)
        {
            foreach (DocumentFileInfo info in list)
            {
                if (!info.IsFolder)
                {
                    lblDocSum.Text = Convert.ToString(Convert.ToInt32(lblDocSum.Text) + 1);
                }
            }
        }
    }

    public string GetChar(object objID)
    {
        DocumentFileInfo info = Yipusoft.BaseDoc.BLL.FileManager.GetFile(new Guid(objID.ToString()));
        return "".PadLeft(2 * info.Level.Value, '　');
    }
    //public string GetChar(object objID)
    //{
    //    string result = "";
    //    DocumentFileInfo info = Yipusoft.BaseDoc.BLL.FileManager.GetFile(new Guid(objID.ToString()));
    //    int level = info.Level.Value;
    //    if (level > 0)
    //    {
    //        result = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|";


    //        for (int k = 1; k < level; k++)
    //        {
    //            result = result + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|";
    //        }
    //        result = result + "—";
    //    }

    //    return result;
    //}

    private void bindDepartment()
    {
        ddlDepartment.DataSource = Department.GetList(" DepartmentId like '%000' and Departmentid <>'1300000'");
        ddlDepartment.DataBind();
        bindSelection();
    }
    protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindSelection();
    }
    protected void ddlSection_SelectedIndexChanged(object sender, EventArgs e)
    {
        DepartmentID = ddlSection.SelectedValue;
        ListPageContral1.SetFirstPage();
        Bind();
    }

    void bindSelection()
    {
        if (ddlDepartment.SelectedValue != "")
        {
            string departmentid = ddlDepartment.SelectedValue;
            ddlSection.DataSource = Department.GetList("DepartmentId like '" + departmentid.Substring(0, 4) + "%'");
            ddlSection.DataBind();
        }
        else
        {
            ddlSection.Items.Clear();
        }
    }
    protected void gvwFileList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        LinkButton btn = e.Row.FindControl("LinkButton1") as LinkButton;
        if (btn == null)
            return;
        btn.PostBackUrl = "DocDepartmentList1.aspx?Year=" + YearList.SelectedValue + "&SM=" + StartMonth.SelectedValue + "&EM=" + EndMonth.SelectedValue + "&DepartmentID=" + ddlSection.SelectedValue + "&ID=" + btn.CommandArgument;
    }

    protected void LinkButton1_Command(object sender, CommandEventArgs e)
    {
        DocumentFileInfo info = Yipusoft.BaseDoc.BLL.FileManager.GetFile(new Guid(e.CommandArgument.ToString()));
        if (info.Format == "Folder")
            Response.Redirect("DocDepartmentList1.aspx?Year=" + YearList.SelectedValue + "&SM=" + StartMonth.SelectedValue + "&EM=" + EndMonth.SelectedValue + "&DepartmentID=" + ddlSection.SelectedValue + "&ID=" + e.CommandArgument.ToString());
        else
            DownLoad(info.NewFileName, info.OldFileName, info.FilePath);

    }

    private void DownLoad(string name, string oldFileName, string filePath)
    {
        string fileName = name;//客户端保存的文件名       

        FileInfo fileInfo = new FileInfo(Server.MapPath(filePath));
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
        Response.AddHeader("Content-Length", fileInfo.Length.ToString());
        Response.AddHeader("Content-Transfer-Encoding", "binary");
        Response.ContentType = "application/octet-stream";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.WriteFile(fileInfo.FullName);
        Response.Flush();
        Response.End();

    }

    protected void YearList_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind();
    }
    protected void StartMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind();
    }
    protected void EndMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind();
    }

    private void bindSubject()
    {
        string DepartmentID = ddlSection.SelectedValue;
        List<DocSearchKeyInfo> list = DocSearchKey.GetList(" DepartmentId='" + DepartmentID + "' ");
        ddlSubject.Items.Clear();
        ddlSubject.Items.Add(new ListItem("----请选择----", ""));
        for (int i = 0; i < list.Count; i++)
        {
            ddlSubject.Items.Add(new ListItem(list[i].KeyWord, list[i].Id.ToString()));
        }
    }

    protected void ddlSubject_TextChanged(object sender, EventArgs e)
    {
        if (ddlSubject.SelectedItem.Text.Trim() != "")
        {
            string DepartmentID = ddlSection.SelectedValue;
            string ParentID = "";
            if (Request["ID"] != null)
            {
                ParentID = Request["ID"];
            }
            Response.Redirect("DocDepartmentResult.aspx?Key=" + Server.UrlEncode(ddlSubject.SelectedItem.Text) + "&Year=" + YearList.SelectedValue + "&SM=" + StartMonth.SelectedValue + "&EM=" + EndMonth.SelectedValue + "&DepartmentID=" + DepartmentID + "&ParentID=" + ParentID);
        }
    }
    protected void lkbZX_Click(object sender, EventArgs e)
    {
        string IDs = "";
        // foreach (GridViewRow row in gvwFileList.Rows)
        for (int i = 0; i < gvwFileList.Rows.Count; i++)
        {
            GridViewRow row = gvwFileList.Rows[i];
            CheckBox cbx = row.FindControl("CheckBox1") as CheckBox;
            if (cbx.Checked)
            {
                IDs += "'" + gvwFileList.DataKeys[i].Value + "',";
            }
        }

        if (IDs.Length > 0)
        {
            IDs = IDs.Remove(IDs.Length - 1);
            string departmentid = ddlSection.SelectedValue;
            Response.Redirect("DocDepartmentResult.aspx?IDs=" + Server.UrlEncode(IDs) + "&Year=" + YearList.SelectedValue + "&DepartmentID=" + departmentid);
        }

        Bind();
    }
    protected void lbtnSearch_Click(object sender, EventArgs e)
    {
        if (txtKey.Text.Trim() != "")
        {
            string DepartmentID = ddlSection.SelectedValue;
            string ParentID = "";
            if (Request["ID"] != null)
            {
                ParentID = Request["ID"];
            }
            Response.Redirect("DocDepartmentResult.aspx?Key=" + Server.UrlEncode(ddlSubject.SelectedItem.Text) + "&Year=" + YearList.SelectedValue + "&SM=" + StartMonth.SelectedValue + "&EM=" + EndMonth.SelectedValue + "&DepartmentID=" + DepartmentID + "&ParentID=" + ParentID);
        }
    }
}
